const getAllExams = () => {
    return localStorage.getItem('examData')
        ? JSON.parse(localStorage.getItem('examData'))
        : []
}
const setAllExams = (data) => {
    localStorage.setItem('examData', JSON.stringify(data))
}

// 查询数据
export const selectExamAsync = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            const examData = getAllExams()
            resolve({ code: 0, data: examData })
        }, 500)
    })
}

// 添加数据
export const createExamAsync = (data) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let examData = getAllExams()
            examData.push(data)
            examData.sort((a, b) => {
                return +a.no - +b.no
            })
            setAllExams(examData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 修改数据
export const updateExamAsync = (data, oldData) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let examData = getAllExams()
            examData = examData.filter((item) => item.no !== oldData.no)
            examData.push(data)
            examData.sort((a, b) => {
                return +a.no - +b.no
            })
            setAllExams(examData)
            resolve({ code: 0 })
        }, 500)
    })
}

// 删除数据
export const deleteExamAsync = (no) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            let examData = getAllExams()
            examData = examData.filter((item) => item.no !== no)
            setAllExams(examData)
            resolve({ code: 0 })
        }, 500)
    })
}
